#include "notepadwindow.h"

notepadWindow::notepadWindow(QWidget *parent) : QMainWindow(parent)
{
    initMenu();
    initTextEdit();
    initStatusBar();
}

void notepadWindow::initMenu()
{
    //菜单栏
    main_bar = menuBar();

    //菜单项
    fileMenu = main_bar->addMenu("日志文件菜单(&F)");  //文件菜单
    editMenu = main_bar->addMenu("编辑菜单(&E)");   //编辑菜单
    checkMenu = main_bar->addMenu("查看菜单(&V)");   //查看菜单

    //菜单项功能

    openAction = fileMenu->addAction("打开文件(&O)"); //打开文件
    saveAction = fileMenu->addAction("另保存"); //保存文件
    connect(saveAction,&QAction::triggered,this,&notepadWindow::saveFile);
    openAction->setShortcut(QKeySequence("ctrl+o"));  //设置快捷键
    //功能
    connect(openAction,&QAction::triggered,[=](){
        filename = QFileDialog::getOpenFileName(this,
                                                        "打开",
                                                        "../",
                                                        "yyyy-MM-dd.txt",
                                                        nullptr,
                                                        QFileDialog::ShowDirsOnly);
        //实例化QFile 对象
        QFile file(filename);
        //        QFile file("://qss/pink.qss");
        //打开文件
        bool isopen = file.open(QIODevice::ReadOnly);
        //判断文件是否打开成功
        if(!isopen)
        {

        }
        //读取qss文件

        /*
        //按行读取文件数据
        while(!file.atEnd())
        {
            QByteArray text = file.readLine();
            //读取文件内容
                     QByteArray text = file.readAll();
            //将读取的文件显示在文本框
            textEdit->append(text);
        }
        */
        //读取文件内容
        QByteArray text = file.readAll();
        //将读取的文件显示在文本框
        textEdit->append(text);
//      QByteArray text = file.readAll();
        //应用样式列表
//       this->setStyleSheet(text);
        //关闭文件
        file.close();

    });
    fileMenu->addSeparator();  //添加分割线

    closeAction = fileMenu->addAction("关闭(&X)"); //关闭
    closeAction->setShortcut(QKeySequence("ctrl+x"));  //设置快捷键
    connect(closeAction, &QAction::triggered, this, &notepadWindow::closeFile);

    copyAction = editMenu->addAction("复制(&C)");  //复制
    copyAction->setShortcut(QKeySequence("ctrl+c"));  //设置快捷键
    connect(copyAction, &QAction::triggered, this, &notepadWindow::copyText);
    editMenu->addSeparator();  //添加分割线

    paseAction = editMenu->addAction("粘贴(&P)");   //粘贴
    paseAction->setShortcut(QKeySequence("ctrl+p"));  //设置快捷键
    connect(paseAction, &QAction::triggered, this, &notepadWindow::pasteText);
    editMenu->addSeparator();  //添加分割线

    allAction = editMenu->addAction("全选(&L)");   //全选
    allAction->setShortcut(QKeySequence("ctrl+l"));  //设置快捷键
    connect(allAction, &QAction::triggered, this, &notepadWindow::selectAllText);
    editMenu->addSeparator();  //添加分割线

    insertTimeAction = editMenu->addAction("插入时间日期(&T)");   //插入时间
    insertTimeAction->setShortcut(QKeySequence("ctrl+t"));  //设置快捷键
    connect(insertTimeAction, &QAction::triggered, this, &notepadWindow::insertTime);
    editMenu->addSeparator();  //添加分割线

    textAction = editMenu->addAction("设置字体");   //字体修改
    connect(textAction, &QAction::triggered, this, &notepadWindow::changeFont);

    scaleMenu = checkMenu->addMenu("缩放");

    bigAction = scaleMenu->addAction("放大");
    bigAction->setShortcut(QKeySequence("ctrl+shift+"));  //设置快捷键
    connect(bigAction, &QAction::triggered, this, &notepadWindow::scaleBig);
    scaleMenu->addSeparator();  //添加分割线

    smallAction = scaleMenu->addAction("缩小");
    smallAction->setShortcut(QKeySequence("ctrl+shift-"));  //设置快捷键
    connect(smallAction, &QAction::triggered, this, &notepadWindow::scaleSmall);
}

void notepadWindow::initTextEdit()
{
    textEdit = new QTextEdit(this);
    setCentralWidget(textEdit);
    connect(textEdit, &QTextEdit::textChanged, this, &notepadWindow::updateStatusBar);
}

void notepadWindow::initStatusBar()
{
    statusBar = new QStatusBar(this);
    setStatusBar(statusBar);
    updateStatusBar();
}

void notepadWindow::closeFile()
{
    textEdit->clear();
}

void notepadWindow::copyText()
{
    textEdit->copy();
}

void notepadWindow::pasteText()
{
    textEdit->paste();
}

void notepadWindow::selectAllText()
{
    textEdit->selectAll();
}

void notepadWindow::insertTime()
{
    textEdit->insertPlainText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}

void notepadWindow::changeFont()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, this);
    if (ok)
    {
        textEdit->setFont(font);
    }
}

void notepadWindow::scaleBig()
{
    textEdit->zoomIn();
}

void notepadWindow::saveFile()
{
    /* 保存*/
#if 0
    QFile  file(filename);
    file.open(QIODevice::WriteOnly);
    file.write(textEdit->toPlainText().toUtf8());
    file.close();
#endif
    /* 另存为*/
    QString otherName = QFileDialog::getSaveFileName(this,"保存",".","all(*.*)");
    QFile  file(otherName);
    file.open(QIODevice::WriteOnly);
    file.write(textEdit->toPlainText().toUtf8());
    file.close();
}

void notepadWindow::scaleSmall()
{
    textEdit->zoomOut();
}

void notepadWindow::updateStatusBar()
{
    // 获取文本编辑器中的文本内容
    QString text = textEdit->toPlainText();

    // 计算字数（包括英文和中文）
    int charCount = text.length();

    // 获取当前时间
    QString currentTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    statusBar->showMessage("字数: " + QString::number(charCount) + "  时间: " + currentTime);
}
